//
//  YTTimingUtilsView.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/18.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTTimingUtilsView.h"


@implementation YTTimingUtilsView

+(void)showTimingPhoto:(int)time
{
    [YTTimingUtilsView dismissUtilsView];
    YTPhotoTipsLable *photoTimingLabel = [[YTPhotoTipsLable alloc] initWithFrame:CGRectMake((ScreenWidth - 200)/2, (ScreenHeight - 50)/2, 200, 50)];
    photoTimingLabel.textAlignment = NSTextAlignmentCenter;
    photoTimingLabel.font = [UIFont fontWithName:@"Heiti SC" size:45];
    photoTimingLabel.textColor = [UIColor whiteColor];
    photoTimingLabel.tag = 1001;
    [KeyWindow addSubview:photoTimingLabel];
    [photoTimingLabel configTiming:time];
}
+(void)p_layoutSubviews
{
    YTPhotoTipsLable *tips1001 = [KeyWindow viewWithTag:1001];
    if (!tips1001) { return; }
    if([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait||[UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown)
    {//竖屏
        tips1001.frame = CGRectMake((ScreenWidth - 200)/2, (ScreenHeight - 50)/2, 200, 50);
    }else
    {
        tips1001.frame = CGRectMake((ScreenWidth - 50)/2, (ScreenHeight - 200)/2, 50, 200);
    }
}
+(void)dismissUtilsView
{
    YTPhotoTipsLable *tips1001 = [KeyWindow viewWithTag:1001];
    if (tips1001)
    {
        [tips1001 stopTimer];
        [tips1001 removeFromSuperview];
        tips1001 = nil;
    }
}

@end

@interface YTPhotoTipsLable ()
@property(nonatomic, strong)dispatch_source_t timingTimer;
@property(nonatomic, strong)dispatch_source_t delayTimer;
@end

@implementation YTPhotoTipsLable

- (void)drawTextInRect:(CGRect)rect {
    
    CGSize shadowOffset = CGSizeMake(0, 0);
    UIColor *textColor = self.textColor;
    
    CGContextRef c = UIGraphicsGetCurrentContext();
    
    CGContextSaveGState(c);
    CGContextSetShadow(c, shadowOffset, 4);
    CGColorSpaceRef textColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextSetShadowWithColor(c, shadowOffset, 4, [[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0.5] CGColor]);
    [super drawTextInRect:rect];
    
    CGContextSetLineWidth(c, 1);
    CGContextSetLineJoin(c, kCGLineJoinRound);
    CGContextSetTextDrawingMode(c, kCGTextStroke);
    self.textColor = [[UIColor alloc] initWithRed:92/255.0 green:92/255.0 blue:92/255.0 alpha:1.0];
    [super drawTextInRect:rect];
    
    CGContextSetTextDrawingMode(c, kCGTextFill);
    self.textColor = textColor;
    self.shadowOffset = CGSizeMake(0, 0);
    [super drawTextInRect:rect];
    
    CGColorSpaceRelease(textColorSpace);
    
    CGContextRestoreGState(c);
    
}
-(void)configTiming:(int)time
{
    self.timingTimer = [YTTools gcdTimer:1.0];
    __block int num = time;
    dispatch_source_set_event_handler(self.timingTimer, ^
                                      {
                                          self.text = [NSString stringWithFormat:@"%d", num];
                                          num--;
                                          if (num == 0)
                                          {
                                              dispatch_cancel(self.timingTimer);
                                              self.timingTimer = nil;
                                              //            [self removeFromSuperview];
                                          }
                                      });
    dispatch_resume(self.timingTimer);
    
}

-(void)configDelay:(NSInteger)time
{
    self.delayTimer = [YTTools gcdTimer:1.0];
    __block NSInteger num = time;
    dispatch_source_set_event_handler(self.delayTimer, ^
                                      {
                                          NSString *str = [YTTools getHHMMSSFromSS:num];
                                          self.text = str;
                                          num++;
                                      });
    dispatch_resume(self.delayTimer);
}
-(void)stopTimer
{
    if (self.timingTimer)
    {
        dispatch_cancel(self.timingTimer);
        self.timingTimer = nil;
    }
    if (self.delayTimer)
    {
        dispatch_cancel(self.delayTimer);
        self.delayTimer = nil;
    }
}
@end
